package com.comita.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;

public class EditMovie extends com.comita.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testEditMovie() throws Exception {
    driver.get(baseUrl + "/php4dvd/");
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.nocover")));
    driver.findElement(By.cssSelector("div.nocover")).click();
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("img[alt=\"Edit\"]")));
    driver.findElement(By.cssSelector("img[alt=\"Edit\"]")).click();
    wait.until(ExpectedConditions.elementToBeClickable(By.name("name")));
    driver.findElement(By.name("name")).clear();
    driver.findElement(By.name("name")).sendKeys("The Best Movie of Year");
    driver.findElement(By.name("year")).clear();
    driver.findElement(By.name("year")).sendKeys("2015");
    driver.findElement(By.name("rating")).clear();
    driver.findElement(By.name("rating")).sendKeys("2.4");
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("img[alt=\"Save\"]")));
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h2")));
    assertEquals("The Best Movie of Year (2015)", driver.findElement(By.cssSelector("h2")).getText());
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

